package com.demo.test02;

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;

import com.alibaba.fastjson.JSONObject;
import com.demo.elasticsearch.RandomUser;

public class Demo03Test {
	private static final String cluster_name = "elasticsearch";
	private static final String hostname = "127.0.0.1";
	private static int port = 9300;
	private static final String index = "demo";
	private static final String type = "articles";
	private static TransportClient client;
	
	public static void init(){
		Settings settings = Settings.settingsBuilder()
				.put("cluster.name", cluster_name)
				.put("client.transport.ignore_cluster_name", true)
				.build();
		client = TransportClient
				.builder()
				.settings(settings)
				.build()
				.addTransportAddress(
						new InetSocketTransportAddress(new InetSocketAddress(
								hostname, port)));
	}
	
	public static void doSearch(String queryString) throws InterruptedException, ExecutionException {
		QueryBuilder nameQueryBuilder = QueryBuilders.queryStringQuery(queryString);//
		SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);// 所有index中查询
		SearchResponse nameSearchResponse = searchRequestBuilder
				.setQuery(nameQueryBuilder).execute().get();
		int i = 0;
		for (SearchHit shit : nameSearchResponse.getHits()) {
			i++;
			System.out.println(i + "\t" + shit.getScore() + "\t"
					+ shit.getSource().get("title"));// shit.getSource
		}
	}
	
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		init();
		saveDatasToElasticSearch();
		//doSearch("入门");
	}
	
	
	private static String[][] datas = {
		{"elasticsearch2.3.3以及其插件离线安装","http://chenghuiz.iteye.com/blog/2312009"},
		{"网络分区带给分布式数据库的难题如何解决？","http://os.51cto.com/art/201307/403298.htm"},
		{"(转)浅谈分布式系统的基本问题：可用性与一致性", "http://chenghuiz.iteye.com/blog/2313894"},
		{"Elasticsearch2.3入门 ", "http://blog.csdn.net/LG772EF/article/details/51925784"},
		{"Elasticsearch: 权威指南","https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html"}
		};
	
	public static void saveDatasToElasticSearch(){
		
		for(String[] str:datas){
			JSONObject obj = new JSONObject();
			obj.put("title", str[0]);
			obj.put("url", str[1]);
			obj.put("datas", RandomUser.doGet(str[1]));
			IndexResponse response = client.prepareIndex(index, type)
					.setSource(obj.toJSONString()).get();
			System.out.println(response.isCreated());
		}
	}

}
